﻿using Core.Entities;
using Core.Interfaces;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Web.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CunstomerController : ControllerBase
    {
        private readonly ICustomerRepository _customerRepository;

        public CunstomerController(ICustomerRepository customerRepository)
        {
            _customerRepository = customerRepository;
        }

        [HttpGet("GetAll")]
        public async Task<IActionResult> GetAll() {
            var customers= await _customerRepository.GetAllAsync();
            return Ok( customers);
        }

        [HttpGet]
        public async Task<IActionResult> Get(int id) { 
            var customer= await _customerRepository.GetByIdAsync(id);
            return Ok( customer);
         }

        [HttpPost]
        public async Task<IActionResult> AddCustomer(Customer customer) { 
             var c= await _customerRepository.AddAsync(customer);
            return Ok( c);
        }

        [HttpPut]
        public async Task<IActionResult> UpdateCustomer(Customer customer)
        {
            var c = await _customerRepository.UpdateAsync(customer);
            return Ok(c);
        }

        [HttpDelete]
        public async Task<IActionResult> DeleteCustomer(int id)
        {
             var c = await _customerRepository.GetByIdAsync(id);
            if (c != null)
            {
                await _customerRepository.DeleteAsync(id);
                return Ok();
            }
            return NotFound();
            
        }

    }
}
